<?php
declare (strict_types=1);

namespace app\command;

use app\data\model\CmsCatModel;
use app\data\model\CmsModel;
use app\model\Blog;
use app\model\BlogCat;
use think\console\Command;
use think\console\Input;
use think\console\Output;

class ImportBlog extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('import_blog')
            ->setDescription('导入新闻');
    }

    protected function execute(Input $input, Output $output)
    {
        $cmsModel = new CmsModel();
        $cmsCatModel = new CmsCatModel();
        $blogModel = new Blog();
        $blogCatModel = new BlogCat();
        // 清空数据
        $cmsCatModel->where(true)->delete();
        $cmsModel->where(true)->delete();
        $output->writeln('数据已清空');

        $root_cat_id = 212;
        $cat_ids = [];
        $blogCatModel->where('parent_id', $root_cat_id)->chunk(10, function ($cats) use (
            $cmsCatModel, $output, &$cat_ids
        ) {
            $insertData = [];
            foreach ($cats as $cat) {
                $rewrite_url = $cat['urlrewriter'] ? 'article/' . $cat['urlrewriter'] : 'article/' . $cat['cat_id'];
                $insertData[] = [
                    "cat_id" => $cat['cat_id'],
                    "parent_id" => 0,
                    "cat_name" => $cat['cat_name'],
                    "sort_order" => $cat['sort_order'],
                    "urlrewriter" => $rewrite_url,
                    "pagetitle" => $cat['pagetitle'],
                    "pagekeywords" => $cat['pagekeywords'],
                    "pagedesc" => $cat['pagedesc'],
                    "pic" => $cat['pic'],
                    "en_cat_name" => $cat['en_cat_name'],
                    'ver' => 0,
                    'create_time' => time(),
                    'update_time' => time(),
                    'delete_time' => 0,
                    'is_show' => 1
                ];
                $cat_ids[] = $cat['cat_id'];

                updateRoute(
                    $cat['cat_name'],
                    '/index/page/blog',
                    strval($rewrite_url),
                    strval($rewrite_url),
                    ['id' => $cat['cat_id']],
                    'blog'
                );
            }
            $result = $cmsCatModel->insertAll($insertData);
            $output->writeln('新闻分类导入成功,数量：' . $result);

        });

        $blogModel->whereIn('cat_id', $cat_ids)->chunk(10, function ($blogs) use (
            $cmsModel, $output
        ) {
            $insertData = [];
            foreach ($blogs as $blog) {
                $rewrite_url = $blog['urlrewriter'] ? 'articledetail/' . $blog['urlrewriter'] : 'articledetail/' . $blog['article_id'];
                $insertData[] = [
                    "article_id" => $blog['article_id'],
                    "cat_id" => $blog['cat_id'],
                    "title" => $blog['title'],
                    "fu_title" => $blog['fu_title'],
                    "title_style" => $blog['title_style'],
                    "content" => $blog['content'],
                    "content2" => $blog['content2'],
                    "author" => $blog['author'],
                    "source" => $blog['source'],
                    "editor" => $blog['editor'],
                    "is_top" => $blog['is_top'],
                    "is_best" => $blog['is_best'],
                    "is_hot" => $blog['is_hot'],
                    "is_show" => $blog['is_show'],
                    "link" => $blog['link'],
                    "photo" => $blog['photo'],
                    "photo2" => $blog['photo2'],
                    "des" => $blog['des'],
                    "urlrewriter" => $rewrite_url,
                    "add_time" => $blog['add_time'],
                    "showtime" => $blog['showtime'],
                    "click_count" => $blog['click_count'],
                    "pagetitle" => $blog['pagetitle'],
                    "pagekeywords" => $blog['pagekeywords'],
                    "pagedesc" => $blog['pagedesc'],
                    "sort_order" => $blog['sort_order'],
                    "ver" => 0,
                    "create_time" => time(),
                    "update_time" => time(),
                    "delete_time" => 0,
                ];
                updateRoute(
                    $blog['article_id'],
                    '/index/page/bloginfo',
                    strval($rewrite_url),
                    strval($rewrite_url),
                    ['id' => $blog['article_id']],
                    'blog'
                );
            }
            $result = $cmsModel->insertAll($insertData);
            $output->writeln('新闻导入成功,数量：' . $result);

        });

        $output->writeln('结束新闻导入');
    }
}
